package com.shy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shy.config.common.BaseQuery;
import com.shy.config.util.AuthUtils;
import com.shy.entity.Clue;
import com.shy.entity.Customer;
import com.shy.mapper.ClueMapper;
import com.shy.mapper.CustomerMapper;
import com.shy.service.CustomerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private ClueMapper clueMapper;
    @Override
    public Boolean saveCustomer(Customer customer) {
        // 修改线索的状态为“已转换为客户”
        Clue clue = new Clue();
        clue.setId(customer.getClueId());
        clue.setState(-1);
        clue.setEditTime(new Date());
        int i = clueMapper.updateByPrimaryKeySelective(clue);
        if (i <= 0) {
            throw new RuntimeException("修改线索状态失败");
        }
        customer.setCreateBy(AuthUtils.getLoginSecurityUserId());
        customer.setCreateTime(new Date());
        return customerMapper.insert(customer)>0;
    }

    @Override
    public PageInfo<Customer> queryCustomerList(Integer current, Integer pageSize) {
        //设置分页
        PageHelper.startPage(current, pageSize);
        //查询数据
        List<Customer> customerlist = customerMapper.queryCustomerList(new BaseQuery());
        //创建分页对象
        PageInfo<Customer> pageInfo = new PageInfo<>(customerlist);
        return pageInfo;
    }

    @Override
    public List<Customer> queryCustomerLists(List<Integer> ids) {
        return customerMapper.queryCustomerByIds(new BaseQuery(),ids);
    }
    @Override
    public List<Customer> queryCustomerList() {
        return customerMapper.queryCustomerList(new BaseQuery());
    }

    @Override
    public Customer queryCustomerById(Integer id) {
        return customerMapper.selectByid(id);
    }
}
